package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Author BrookPeng
 * @Date 2020/11/25 21:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/list")
public class ListApiController {

    @Autowired
    private ListService listService;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate; // 高级别的客户端

    // @Autowired
    // private ElasticsearchTemplate elasticsearchTemplate; // 低级别客户端

    // 创建映射Mapping
    @GetMapping("/mappings")
    public Result mappings() {

        // 创建索引库
        elasticsearchRestTemplate.createIndex(Goods.class);
        // 映射表关系
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    // 上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId) {

        listService.onSale(skuId);
        return Result.ok();
    }

    // 下架
    @GetMapping("/offSale/{skuId}")
    public Result offSale(@PathVariable Long skuId) {

        listService.offSale(skuId);
        return Result.ok();
    }

    // 商品热度更新
    @GetMapping("/inner/hotScore/{skuId}")
    public Result hotScore(@PathVariable("skuId") Long skuId) {

        listService.hotScore(skuId);
        return Result.ok();
    }

    // 开始搜索 http://.... /api/list
    @PostMapping
    public SearchResponseVo search(@RequestBody SearchParam searchParam) {

        return listService.list(searchParam);
    }
}
